package org.clockwise.util;

public class MyStrUtil {
	public static String getRidOfLabels(String source) {
		int l = source.indexOf("<"), r = source.indexOf(">");
		while (l >= 0 && l < r) {
			source = source.substring(0, l) + source.substring(r + 1, source.length());
			l = source.indexOf("<");
			r = source.indexOf(">");
		}
		return source;
	}
	
	public static String getRidOf(String line, String badStr) {
		if (!line.contains(badStr)) {
			return line;
		}
		String[] seg = line.split(badStr);
		if (seg.length == 1) {
			return seg[0];
		}
		else {
			StringBuilder sb = new StringBuilder();
			for (int i = 1; i < seg.length; i++) {
				sb.append(seg[i]);
			}
			return sb.toString();			
		}
	}
	
	/*
	 * This function is cited from web
	 * http://blog.csdn.net/iloveyou1098/article/details/6606486#
	 */
	public static String comSubstring(String str1, String str2) {
		char[] a = str1.toCharArray();
		char[] b = str2.toCharArray();
		int a_length = a.length;
		int b_length = b.length;
		int[][] lcs = new int[a_length + 1][b_length + 1];
		// 初始化数组
		for (int i = 0; i <= b_length; i++) {
			for (int j = 0; j <= a_length; j++) {
				lcs[j][i] = 0;
			}
		}
		for (int i = 1; i <= a_length; i++) {
			for (int j = 1; j <= b_length; j++) {
				if (a[i - 1] == b[j - 1]) {
					lcs[i][j] = lcs[i - 1][j - 1] + 1;
				}
				if (a[i - 1] != b[j - 1]) {
					lcs[i][j] = lcs[i][j - 1] > lcs[i - 1][j] ? lcs[i][j - 1]
							: lcs[i - 1][j];
				}
			}
		}
	
		// 由数组构造最小公共字符串
		int max_length = lcs[a_length][b_length];
		char[] comStr = new char[max_length];
		int i =a_length, j =b_length;
		while(max_length>0){
			if(lcs[i][j]!=lcs[i-1][j-1]){
				if(lcs[i-1][j]==lcs[i][j-1]){//两字符相等，为公共字符
					comStr[max_length-1]=a[i-1];
					max_length--;
					i--;j--;
				}else{//取两者中较长者作为A和B的最长公共子序列
					if(lcs[i-1][j]>lcs[i][j-1]){
						i--;
					}else{
						j--;
					}
				}
			}else{
				i--;j--;
			}
		}
//		System.out.print("最长公共字符串是：");
//		System.out.print(comStr);
		return new String(comStr);
	}
	
	public static String mergeStrArray(String[] array, String separator) {
		StringBuilder sb = new StringBuilder();
		if (array == null || array.length == 0) {
			return "";
		}
		sb.append(array[0]);
		for (int i = 1; i < array.length; i++) {
			sb.append(separator);
			sb.append(array[i]);
		}
		return sb.toString();
	}
	
	public static String mergeStrArray(int[] array, String separator) {
		StringBuilder sb = new StringBuilder();
		if (array == null || array.length == 0) {
			return "";
		}
		sb.append(array[0]);
		for (int i = 1; i < array.length; i++) {
			sb.append(separator);
			sb.append(array[i]);
		}
		return sb.toString();
	}
	
	public static int[] splitStrToInt(String line, String separator) {
		String[] seg = line.split(separator);
		int[] a = new int[seg.length];
		for (int i = 0; i < seg.length; i++) {
			try {
				a[i] = Integer.parseInt(seg[i]);
			} catch (NumberFormatException e) {
				System.out.println("Error! Fail to split string to int[]!");
				System.out.println(line);
				return null;
			}
		}
		return a;
	}
	
	static String separator = null;
	public static String getLineSeparator() {
		if (separator == null) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < 30; i++) {
				sb.append('*');
			}
			separator = sb.toString();
		}
		return separator;
	}
	
}
